//编写代码，以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前 。
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        struct ListNode* lesshead, * lesstail, * greaterhead, * greatertail;
        lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));
        greaterhead = greatertail = (struct ListNode*)malloc(sizeof(struct ListNode));
        lesstail->next = NULL;
        greatertail->next = NULL;
        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                lesstail->next = cur;
                lesstail = lesstail->next;
                cur = cur->next;
            }
            else
            {
                greatertail->next = cur;
                greatertail = greatertail->next;
                cur = cur->next;
            }


        }
        greatertail->next = NULL;
        lesstail->next = greaterhead->next;
        struct ListNode* head = lesshead->next;
        free(lesshead);
        free(greaterhead);
        return head;
    }
};
//7. 链表的回文结构。
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class PalindromeList {
public:
    struct ListNode* middleNode(struct ListNode* head)
    {
        struct ListNode* slow, * fast;
        slow = fast = head;
        while (fast && fast->next)
        {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }
    struct ListNode* reverseList(struct ListNode* head)
    {
        if (head == NULL)
            return NULL;
        struct ListNode* n1, * n2, * n3;
        n1 = NULL;
        n2 = head;
        n3 = n2->next;
        while (n2)
        {
            n2->next = n1;
            n1 = n2;
            n2 = n3;
            if (n3)
            {
                n3 = n3->next;
            }
        }
        return n1;
    }
    bool chkPalindrome(ListNode* A) {
        // write code here
        struct ListNode* head = A;
        struct ListNode* mid = middleNode(head);
        struct ListNode* rhead = reverseList(mid);
        while (head && rhead)
        {
            if (head->val != rhead->val)
            {
                return false;
            }
            else
            {
                head = head->next;
                rhead = rhead->next;
            }

        }
        return true;
    }
};
//8. 输入两个链表，找出它们的第一个公共结点。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    if (headA == NULL || headB == NULL)
        return NULL;
    int lenA = 0;
    int lenB = 0;
    struct ListNode* curA = headA;
    struct ListNode* curB = headB;
    while (curA)
    {

        curA = curA->next;
        lenA++;
    }
    while (curB)
    {
        curB = curB->next;
        lenB++;
    }
    // struct ListNode*front,*behind;
    // if(lenA>lenB)
    // {
    //     front=headA;
    //     behind=headB;
    // }
    // else
    // {
    //     front=headB;
    //     behind=headA;
    // }
    struct ListNode* front = headA, * behind = headB;
    if (lenA < lenB)
    {
        front = headB;
        behind = headA;
    }
    int gap = abs(lenA - lenB);
    while (gap--)
    {
        front = front->next;
    }
    while (front != behind)
    {
        front = front->next;
        behind = behind->next;
    }
    return front;
}
//9. 给定一个链表，判断链表中是否有环。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode* head) {
    struct ListNode* fast = head, * slow = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (fast == slow)
            return true;
    }
    return false;

}